﻿using AutoTest.Business.Dto.UserInfo;
using AutoTest.Business.IServices;
using AutoTest.Business.IServices.Common;
using AutoTest.Business.Vo.UserPlanConfig;
using AutoTest.Common.UtilsHelper;
using AutoTest.Framework.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AutoTest.WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("db")]
    public class DatabaseController(IDatabaseService databaseService,IUserService userService) : Controller
    {
        private readonly IDatabaseService _databaseService = databaseService;
        private readonly IUserService _userService = userService;

        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost("init")]
        [AllowAnonymous]
        public ApiResult CreateDb()
        {
            return ApiResult.FromResult(() => _databaseService.CreateTables());
        }

        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost("create-table")]
        [AllowAnonymous]
        public ApiResult CreateTable(string tableName)
        {
            return ApiResult.FromResult(() => _databaseService.CreateTable(tableName));
        }

        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost("create-setting")]
        [AllowAnonymous]
        public async Task<ApiResult> CreateAppSetting()
        {
            return await ApiResult.FromResult(async () => await _databaseService.CreateAppSetting());
        }
        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost("create-setting/{optionKeyName}")]
        [AllowAnonymous]
        public async Task<ApiResult> CreateAppSetting([FromRoute]string optionKeyName)
        {
            return await ApiResult.FromResult(async () => await _databaseService.CreateAppSetting(optionKeyName));
        }
        /// <summary>
        /// 数据库初始化
        /// </summary>
        /// <returns></returns>
        [HttpPost("get-id")]
        [AllowAnonymous]
        public ApiResult<long> GetId()
        {
            return ApiResult<long>.Successed(IdHelper.NextId());
        }
        /// <summary>
        /// 保存Data
        /// </summary>
        /// <returns></returns>
        [HttpPost("save/user/plan/config")]
        public async Task<ApiResult> SaveUserPlanConfig([FromBody]UserPlanConfigAddDto addDto)
        {
            await _userService.AddUserPlanConfigAsync(addDto);
            return ApiResult.Successed();
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        [HttpGet("get/user/plan/config/{configId}")]
        public async Task<ApiResult<UserPlanConfigVo>> GetUserPlanConfig([FromRoute] long configId)
        {
            return await ApiResult<UserPlanConfigVo>.Successed(()=>_userService.GetUserPlanConfigAsync(configId));
        }
    }
}
